package com.tour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.tour.config.ResponseWrapper;
import com.tour.entity.Commodity;
import com.tour.entity.DFood;
import com.tour.mapper.CommodityMapper;
import com.tour.service.CommodityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2021-03-30
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity> implements CommodityService {
    @Autowired
    CommodityMapper commodityMapper;

    public ResponseWrapper addCommodity(Commodity commodity){
        if (commodity == null){
            return ResponseWrapper.markError();
        }else {
            int result = commodityMapper.insert(commodity);
            if (result == 1){
                return ResponseWrapper.markSuccess(null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }
    public ResponseWrapper updateCommodity(Commodity commodity){
        if (commodity == null){
            return ResponseWrapper.markError();
        }else {
            int result = commodityMapper.updateById(commodity);
            if (result == 1){
                return ResponseWrapper.markSuccess(null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }

    public ResponseWrapper deleteCommodity(Integer cid){
        if (cid == null){
            return ResponseWrapper.markError();
        }else {
            int result = commodityMapper.deleteById(cid);
            if (result == 1){
                return ResponseWrapper.markSuccess(null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }
    public ResponseWrapper findCommodity(Integer cid){
        if (cid == null){
            return ResponseWrapper.markError();
        }else {
            Commodity commodity = commodityMapper.selectById(cid);
            if (commodity != null){
                ResponseWrapper wrapper = ResponseWrapper.markSuccess(commodity);
                wrapper.getData().put("data",commodity);
                return wrapper;
            }else {
                return ResponseWrapper.markError();
            }
        }
    }

    public ResponseWrapper findAllCommodity(String name,Integer pageSize,Integer pageNum,String place){
        Page<Commodity> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Commodity> restaurantQueryWrapper = new QueryWrapper<>();
        restaurantQueryWrapper.like(!StringUtils.isNullOrEmpty(name),"c_name",name).eq(!StringUtils.isNullOrEmpty(place),"place",place);
        IPage<Commodity> iPage = commodityMapper.selectPage(page,restaurantQueryWrapper);
        List<Commodity> list = iPage.getRecords();
        ResponseWrapper wrapper = ResponseWrapper.markSuccess(list);
        wrapper.getData().put("data",list);
        wrapper.getData().put("total",iPage.getTotal());
        wrapper.getData().put("pageSize",iPage.getSize());
        wrapper.getData().put("pageNum",iPage.getCurrent());
        return wrapper;
    }
}
